import {plugin, common, puppeteer} from '../../api/api.js';
import {
  __PATH, Add_HP,
  Add_修为,
  Add_血气, existshop, Harm,
  Read_danyao, Read_shop,
  Write_danyao, Write_shop,
  zd_battle
} from '../../model/xiuxian.js';
import data from '../../model/XiuxianData.js';
import {
  existplayer,
  Read_player,
  exist_najie_thing,
  Add_najie_thing,
  Write_player,
  shijianc,
} from '../../model/xiuxian.js';
import Show from "../../model/show.js";
import config from "../../model/Config.js";

export class action extends plugin {
  constructor() {
    super({
      /** 功能名称 */
      name: 'action_huodong',
      /** 功能描述 */
      dsc: '交易模块',
      event: 'message',
      /** 优先级，数字越小等级越高 */
      priority: 600,
      rule: [
        {
          reg: '^#闯荡$',
          fnc: 'action_way',
        },
      ],
    });
    this.xiuxianConfigData = config.getConfig('xiuxian', 'xiuxian');
  }

  async action_way(e) {
    await action_way_fun(e);
  }

}

export  async function action_way_fun(e) {
    if (!e.isGroup) {
      return;
    }
    let usr_qq = e.user_id;
    //查看存档
    let ifexistplay = await existplayer(usr_qq);
    if (!ifexistplay) {
      return;
    }
    let game_action = await redis.get(
        'xiuxian:player:' + usr_qq + ':game_action'
    );
    //防止继续其他娱乐行为
    if (game_action == 0) {
      e.reply('修仙：游戏进行中...');
      return;
    }
    //查询redis中的人物动作
    let action = await redis.get('xiuxian:player:' + usr_qq + ':action');
    action = JSON.parse(action);
    let now_time = new Date().getTime();
    if (action != null) {
      let player = await Read_player(usr_qq);
      let get_time = parseInt(now_time - action.start_time);
      //如果是神界状态
      if ('神界' == action.action) {
        let need_time = 1800000;
        let cishu = 1;
        if(null != action.cishu){
          cishu = action.cishu;
          need_time = need_time * cishu;
        }
        if(need_time <= get_time){
          let xiuwei_SP_num;
          let xueqi_SP_num;
          let data_thing = [];
          for (let i = 1; i <= 5 * cishu; i++) {
            var thing_name;
            var thing_class;
            var x = 0.98;
            let random1 = Math.random();
            var y = 0.4;
            let random2 = Math.random();
            var z = 0.15;
            let random3 = Math.random();
            let random4;
            //物品数量
            let num = 1;
            //物品级别
            let SP = `style=color:honeydew`;
            //三种概率获取物品
            if (random1 <= x) {
              if (random2 <= y) {
                if (random3 <= z) {
                  random4 = Math.floor(
                      Math.random() * data.shenjie[0].three.length
                  );
                  thing_name = data.shenjie[0].three[random4].name;
                  thing_class = data.shenjie[0].three[random4].class;
                  xiuwei_SP_num = 2 + Math.random();
                  xueqi_SP_num = 2 + Math.random();
                  SP =`style=color:gold`;
                } else {
                  random4 = Math.floor(
                      Math.random() * data.shenjie[0].two.length
                  );
                  thing_name = data.shenjie[0].two[random4].name;
                  thing_class = data.shenjie[0].two[random4].class;
                  xiuwei_SP_num = 1 + Math.random();
                  xueqi_SP_num = 1 + Math.random();
                  SP = `style=color:springgreen`;
                }
              } else {
                random4 = Math.floor(
                    Math.random() * data.shenjie[0].one.length
                );
                thing_name = data.shenjie[0].one[random4].name;
                thing_class = data.shenjie[0].one[random4].class;
                xiuwei_SP_num = 0.5 + Math.random() * 0.5;
                xueqi_SP_num = 0.5 + Math.random() * 0.5;
                SP =`style=color:honeydew`;
              }
            } else {
              thing_name = '';
              thing_class = '';
              xiuwei_SP_num = 2 + Math.random();
              xueqi_SP_num = 2 + Math.random();
              SP = `style=color:honeydew`;
            }
            //计算是否有幸运双倍
            let random = Math.random();
            if (random < player.幸运) {
                num += 1;
            }
            if (player.islucky > 0) {
              player.islucky--;
              if (player.islucky != 0) {
              } else {
                player.幸运 -= player.addluckyNo;
                player.addluckyNo = 0;
              }
              await data.setData('player', usr_qq, player);
            }
            if (thing_name != '' || thing_class != '') {
              await Add_najie_thing(usr_qq, thing_name, thing_class, num);
            }
            data_thing[i-1] = {
              thing_name:thing_name,
              thing_class:thing_class,
              num:num,
              SP:SP,
              is_lucky:player.islucky,
            };
          }

          //结算增加五次的血气修为
          let now_level_id = player.level_id;
          let now_physique_id = player.Physique_id;
          //结算
          let xiuwei = Math.trunc(
              2000 + (100 * now_level_id * now_level_id * xiuwei_SP_num * 0.1) / 5
          ) * 5 * cishu;
          let qixue = Math.trunc(
              2000 + 100 * now_physique_id * now_physique_id * xueqi_SP_num * 0.1
          ) * 5 * cishu;
          await Add_血气(usr_qq, qixue);
          await Add_修为(usr_qq, xiuwei);
          await  redis.del('xiuxian:player:' + usr_qq + ':action');
          //显示神界战果
          let data_shenjie = {data_thing, xiuwei, qixue,};
          const data1 = await new Show().get_shenJie_Data(data_shenjie);
          let img = await puppeteer.screenshot('shenJie', {
            ...data1,
            type: "shenJie",
          });
          e.reply(img);
          return;
        }else{
          let time = parseInt((need_time - get_time)/60000);
          e.reply(`再闯荡${time}分钟吧，神界大天使在哪`);
          return;
        }//如果是秘境
      } else if('魔界' == action.action) {
        let need_time = 3600000;
        let cishu = 1;
        if(null != action.cishu){
          cishu = action.cishu;
          need_time = need_time * cishu;
        }
        if(need_time <= get_time){
          let xiuwei_SP_num;
          let xueqi_SP_num;
          let data_thing = [];
          for (let i = 1; i <= 10 * cishu; i++) {
            var thing_name;
            var thing_class;
            var x = 0.98;
            let random1 = Math.random();
            var y = 0.4;
            let random2 = Math.random();
            var z = 0.15;
            let random3 = Math.random();
            let random4;
            //物品数量
            let num = 1;
            //物品级别
            let SP = `style=color:honeydew`;
            if (random1 <= x) {
              if (random2 <= y) {
                if (random3 <= z) {
                  random4 = Math.floor(
                      Math.random() * data.mojie[0].three.length
                  );
                  thing_name = data.mojie[0].three[random4].name;
                  thing_class = data.mojie[0].three[random4].class;
                  xiuwei_SP_num = 2 + Math.random();
                  xueqi_SP_num = 2 + Math.random();
                  SP =`style=color:gold`;
                } else {
                  random4 = Math.floor(
                      Math.random() * data.mojie[0].two.length
                  );
                  thing_name = data.mojie[0].two[random4].name;
                  thing_class = data.mojie[0].two[random4].class;
                  xiuwei_SP_num = 1 + Math.random();
                  xueqi_SP_num = 1 + Math.random();
                  SP = `style=color:springgreen`;
                }
              } else {
                random4 = Math.floor(Math.random() * data.mojie[0].one.length);
                thing_name = data.mojie[0].one[random4].name;
                thing_class = data.mojie[0].one[random4].class;
                xiuwei_SP_num = 0.5 + Math.random() * 0.5;
                xueqi_SP_num = 0.5 + Math.random() * 0.5;
                SP =`style=color:honeydew`;
              }
            } else {
              thing_name = '';
              thing_class = '';
              xiuwei_SP_num = 2 + Math.random();
              xueqi_SP_num = 2 + Math.random();
              SP = `style=color:honeydew`;
            }
            //计算是否有幸运双倍
            let random = Math.random();
            if (random < player.幸运) {
              num += 1;
            }
            if (player.islucky > 0) {
              player.islucky--;
              if (player.islucky != 0) {
              } else {
                player.幸运 -= player.addluckyNo;
                player.addluckyNo = 0;
              }
              await data.setData('player', usr_qq, player);
            }
            if (thing_name != '' || thing_class != '') {
              await Add_najie_thing(usr_qq, thing_name, thing_class, num);
            }

            data_thing[i-1] = {
              thing_name:thing_name,
              thing_class:thing_class,
              num:num,
              SP:SP,
              is_lucky:player.islucky,
            };
          }

          //默认10次血气修为
          let now_level_id = player.level_id;
          let now_physique_id = player.Physique_id;
          //结算
          let xiuwei = Math.trunc(
              2000 + (100 * now_level_id * now_level_id * xiuwei_SP_num * 0.1) / 5
          ) * 10 * cishu;
          let qixue = Math.trunc(
              2000 + 100 * now_physique_id * now_physique_id * xueqi_SP_num * 0.1
          ) * 10 * cishu;
          if (10 <= await exist_najie_thing(usr_qq, '修魔丹', '道具')) {
            xiuwei *= 100;
            xiuwei = Math.trunc(xiuwei);
            await Add_najie_thing(usr_qq, '修魔丹', '道具', -10);
          }
          if (10 <= await exist_najie_thing(usr_qq, '血魔丹', '道具')) {
            qixue *= 18;
            qixue = Math.trunc(qixue);
            await Add_najie_thing(usr_qq, '血魔丹', '道具', -10);
          }
          //增加修为和血气
          await Add_血气(usr_qq, qixue);
          await Add_修为(usr_qq, xiuwei);
          await  redis.del('xiuxian:player:' + usr_qq + ':action');
          //显示魔界战果
          let data_mojie = {data_thing,xiuwei,qixue,}
          const data1 = await new Show().get_shenJie_Data(data_mojie);
          let img = await puppeteer.screenshot('shenJie', {
            ...data1,
            type: "shenJie",
          });
          e.reply(img);
          return;
        }else{
          let time = parseInt((need_time-get_time)/60000);
          e.reply(`还想在魔界闯荡${time}分钟呢，说不定能遇到魅魔，我才不出去`)
          return;
        }
      }else if('历练' == action.action){
        let need_time = 60000;
        let cishu = 1;
        let weizhi = action.Place_address;
        if(null != action.cishu){
          cishu = action.cishu;
          need_time = need_time * cishu;
        }
        if(need_time <= get_time){
          let msg = [];
          for(let i=1; i<=cishu; i++){
            if (player.当前血量 < 0.3 * player.血量上限) {
              if (await exist_najie_thing(usr_qq, '起死回生丹', '丹药')) {
                player.当前血量 = player.血量上限;
                await Add_najie_thing(usr_qq, '起死回生丹', '丹药', -1);
                await Write_player(usr_qq, player);
              }//快死扣除丹药
            }
            let A_player = {
              名号: player.名号,
              攻击: player.攻击,
              防御: player.防御,
              当前血量: player.当前血量,
              暴击率: player.暴击率,
              法球倍率: player.灵根.法球倍率,
              仙宠: player.仙宠,
            };
            let buff = 1;
            if (weizhi.name == '大千世界' || weizhi.name == '仙界矿场')
              buff = 0.7;
            let monster_length = data.monster_list.length;
            let monster_index = Math.trunc(Math.random() * monster_length);
            let monster = data.monster_list[monster_index];
            let B_player = {
              名号: monster.名号,
              攻击: parseInt(monster.攻击 * player.攻击 * buff),
              防御: parseInt(monster.防御 * player.防御 * buff),
              当前血量: parseInt(monster.当前血量 * player.血量上限 * buff),
              暴击率: monster.暴击率 * buff,
              法球倍率: 0.1,
              hun_Num:monster.hun_Num,
            };
            let Data_battle = await zd_battle(A_player, B_player);
            let msgg = Data_battle.msg;
            let A_win = `${A_player.名号}击败了${B_player.名号}`;
            let B_win = `${B_player.名号}击败了${A_player.名号}`;
            var thing_name;
            var thing_class;
            var x = 0.99;
            let random1 = Math.random();
            var y = 0.6;
            let random2 = Math.random();
            var z = 0.28;
            let random3 = Math.random();
            let random4;//物品
            var m = '';
            let fyd_msg = '';
            //查找秘境
            let t1;
            let t2;
            var n = 1;
            let last_msg = '';
            if (random1 <= x) {
              if (random2 <= y) {
                if (random3 <= z) {
                  random4 = Math.floor(Math.random() * weizhi.three.length);
                  thing_name = weizhi.three[random4].name;
                  thing_class = weizhi.three[random4].class;
                  m = `抬头一看，金光一闪！有什么东西从天而降，定睛一看，原来是：[${thing_name}`;
                  t1 = 2 + Math.random();
                  t2 = 2 + Math.random();
                } else {
                  random4 = Math.floor(Math.random() * weizhi.two.length);
                  thing_name = weizhi.two[random4].name;
                  thing_class = weizhi.two[random4].class;
                  m = `在洞穴中拿到[${thing_name}`;
                  t1 = 1 + Math.random();
                  t2 = 1 + Math.random();
                  if (weizhi.name == '太极之阳' || weizhi.name == '太极之阴') {
                    n = 5;
                    m = '捡到了[' + thing_name;
                  }
                  if (weizhi.name == '诸神黄昏·旧神界') {
                    if (thing_name == '绝天白灵芝' ||thing_name == '天罗地网') n = 6;
                    m = '捡到了[' + thing_name;
                  }
                }
              } else {
                random4 = Math.floor(Math.random() * weizhi.one.length);
                thing_name = weizhi.one[random4].name;
                thing_class = weizhi.one[random4].class;
                m = `捡到了[${thing_name}`;
                t1 = 0.5 + Math.random() * 0.5;
                t2 = 0.5 + Math.random() * 0.5;
                if (weizhi.name == '诸神黄昏·旧神界') {
                  n = 100;
                  if (thing_name == '洗根水') n = 130;
                  m = '捡到了[' + thing_name;
                }
                if (weizhi.name == '太极之阳' || weizhi.name == '太极之阴') {
                  n = 5;
                  m = '捡到了[' + thing_name;
                }
              }
            } else {
              m = '走在路上看见了一只蚂蚁！蚂蚁大仙送了你[起死回生丹';
              await Add_najie_thing(usr_qq, '起死回生丹', '丹药', 1);
              t1 = 0.5 + Math.random() * 0.5;
              t2 = 0.5 + Math.random() * 0.5;
            }
            if (weizhi.name != '诸神黄昏·旧神界') {
              //判断是不是旧神界
              let random = Math.random();
              if (random < player.幸运) {
                if (random < player.addluckyNo) {
                  last_msg += '福源丹生效，所以在';
                } else if (player.仙宠.type == '幸运') {
                  last_msg += '仙宠使你在探索中欧气满满，所以在';
                }
                n *= 2;
                last_msg += '本次探索中获得赐福加成\n';
              }
              if (player.islucky > 0) {
                player.islucky--;
                if (player.islucky != 0) {
                  fyd_msg = `  \n福源丹的效力将在${player.islucky}次探索后失效\n`;
                } else {
                  fyd_msg = `  \n本次探索后，福源丹已失效\n`;
                  player.幸运 -= player.addluckyNo;
                  player.addluckyNo = 0;
                }
                data.setData('player', usr_qq, player);
              }
            }
            m += `]×${n}个。`;

            let xiuwei = 0;
            //默认结算装备数
            let now_level_id;
            let now_physique_id;
            now_level_id = player.level_id;
            now_physique_id = player.Physique_id;
            //结算
            let qixue = 0;
            if (msgg.find(item => item == A_win)) {
              xiuwei = Math.trunc(
                  2000 + (100 * now_level_id * now_level_id * t1 * 0.1) / 5
              );
              qixue = Math.trunc(
                  2000 + 100 * now_physique_id * now_physique_id * t2 * 0.1
              );
              if (thing_name) {
                await Add_najie_thing(usr_qq, thing_name, thing_class, n);
              }
              last_msg += `${m}不巧撞见[${
                  B_player.名号
              }],经过一番战斗,击败对手,获得修为${xiuwei},气血${qixue},剩余血量${
                  A_player.当前血量 + Data_battle.A_xue
              }`;
              let random = Math.random(); //万分之一出神迹
              let newrandom = 0.995;
              let dy = await Read_danyao(usr_qq);
              newrandom -= dy.beiyong1;
              if (dy.ped > 0) {
                dy.ped--;
              } else {
                dy.beiyong1 = 0;
                dy.ped = 0;
              }
              await Write_danyao(usr_qq, dy);
              if (random > newrandom) {
                let length = data.xianchonkouliang.length;
                let index = Math.trunc(Math.random() * length);
                let kouliang = data.xianchonkouliang[index];
                last_msg +=
                    '\n七彩流光的神奇仙谷[' +
                    kouliang.name +
                    ']深埋在土壤中，是仙兽们的最爱。';
                await Add_najie_thing(usr_qq, kouliang.name, '仙宠口粮', 1);
              }
              if (random > 0.1 && random < 0.1002) {
                last_msg +=
                    '\n' +
                    B_player.名号 +
                    '倒下后,你正准备离开此地，看见路边草丛里有个长相奇怪的石头，顺手放进了纳戒。';
                await Add_najie_thing(usr_qq, '长相奇怪的小石头', '道具', 1);
              }
            } else if (msgg.find(item => item == B_win)) {
              xiuwei = 800;
              last_msg =
                  '不巧撞见[' +
                  B_player.名号 +
                  '],经过一番战斗,败下阵来,还好跑得快,只获得了修为' +
                  xiuwei +
                  ']';
            }
            msg.push('\n' + player.名号 + last_msg + fyd_msg);
            //先完结再结算
            await Add_血气(usr_qq, qixue);
            await Add_修为(usr_qq, xiuwei);
            await Add_HP(usr_qq, Data_battle.A_xue);
            player.当前血量 = A_player.当前血量 + Data_battle.A_xue;
          }
          await redis.del('xiuxian:player:' + usr_qq + ':action');
          //发送消息
          let temp_data = {
            temp: msg,
          };
          const data1 = await new Show().get_tempData(temp_data);
          let img = await puppeteer.screenshot('temp', {
            ...data1,
          });
          e.reply(img);
          return;
        }else{
          let time = parseInt((need_time-get_time)/60000);
          e.reply(`还想在秘境${weizhi.name}闯荡${time}分钟，不出去`);
          return;
        }
      }else if('洗劫' == action.action){
        let need_time = 1500000;
        if (need_time <= get_time){
          let msg = [];
          let weizhi = action.Place_address;
          let i; //获取对应npc列表的位置
          for (i = 0; i < data.npc_list.length; i++) {
            if (data.npc_list[i].name == weizhi.name) {
              break;
            }
          }
          let A_player = action.A_player;
          let monster_length;
          let monster_index;
          let monster;
          if (weizhi.Grade == 1) {
            monster_length = data.npc_list[i].one.length;
            monster_index = Math.trunc(Math.random() * monster_length);
            monster = data.npc_list[i].one[monster_index];
          } else if (weizhi.Grade == 2) {
            monster_length = data.npc_list[i].two.length;
            monster_index = Math.trunc(Math.random() * monster_length);
            monster = data.npc_list[i].two[monster_index];
          } else {
            monster_length = data.npc_list[i].three.length;
            monster_index = Math.trunc(Math.random() * monster_length);
            monster = data.npc_list[i].three[monster_index];
          }
          //设定npc数值
          let B_player = {
            名号: monster.name,
            攻击: parseInt(monster.atk * A_player.攻击),
            防御: parseInt(
                (monster.def * A_player.防御) / (1 + weizhi.Grade * 0.05)
            ),
            当前血量: parseInt(
                (monster.blood * A_player.当前血量) / (1 + weizhi.Grade * 0.05)
            ),
            暴击率: monster.baoji,
            灵根: monster.灵根,
            法球倍率: monster.灵根.法球倍率,
          };
          let Data_battle;
          let last_msg = '';
          if (A_player.魔值 == 0) {
            //根据魔道值决定先后手顺序
            Data_battle = await zd_battle(A_player, B_player);
            last_msg += A_player.名号 + '隐身守护' + B_player.名号;
            A_player.当前血量 += Data_battle.A_xue;
          } else {
            Data_battle = await zd_battle(B_player, A_player);
            last_msg +=
                A_player.名号 + '杀气过重,守护' + B_player.名号 + '时被发现了';
            A_player.当前血量 += Data_battle.B_xue;
          }
          let msgg = Data_battle.msg;
          //console.log(msgg);
          let A_win = `${A_player.名号}击败了${B_player.名号}`;
          let B_win = `${B_player.名号}击败了${A_player.名号}`;
          let arr = action;
          if (msgg.find(item => item == A_win)) {
            arr.A_player = A_player;
            arr.action = '洗劫';
            arr.start_time =new Date().getTime();
            last_msg +=
                ',经过一番战斗,击败竞镖者,剩余' +
                A_player.当前血量 +
                '血量,商讨护镖完成后的奖励';
            let thing = await existshop(weizhi.name);
            let thing_name = [];
            let shop = await Read_shop();
            let i;
            for (i = 0; i < shop.length; i++) {
              if (shop[i].name == weizhi.name) {
                break;
              }
            }
            if (!thing) {
              shop[i].state = 0;
              await Write_shop(shop);
              //没有物品,进入下一阶段
              last_msg += '已经被领取完了';
              await redis.del('xiuxian:player:' + usr_qq + ':action');
              await redis.del('xiuxian:player:' + weizhi.id + ':xijieend');
              msg.push('\n' + last_msg);
              e.reply(msg);
              return;
            } else {
              let x = shop[i].Grade * 2;
              while (x > 0 && thing != false) {
                var t; //临时存储物品名
                var thing_index = Math.trunc(Math.random() * thing.length);
                t = thing[thing_index];
                thing_name.push(t);
                shop = await Read_shop();
                for (var j = 0; j < shop[i].one.length; j++) {
                  if (
                      shop[i].one[j].name == t.name &&
                      shop[i].one[j].数量 > 0
                  ) {
                    shop[i].one[j].数量 = 0;
                    await Write_shop(shop);
                    break;
                  }
                }
                thing = await existshop(weizhi.name);
                x--;
              }
              last_msg += '经过一番商讨' + arr.A_player.名号 + '给与了';
              for (var j = 0; j < thing_name.length; j++) {
                last_msg +=
                    '\n' + thing_name[j].name + ' x ' + thing_name[j].数量;
              }
              last_msg +=
                  '\n刚出门就被深渊的人盯上了,他们仗着人多，你一夫当关，枪出如龙';
            }
            arr.action = '洗劫';
            arr.start_time = new Date().getTime();
            arr.thing = thing_name;
            arr.cishu = shop[i].Grade + 1;
            //写入redis
            await redis.set(
                'xiuxian:player:' + usr_qq + ':action',
                JSON.stringify(arr)
            );
            for( var k = 0; k < arr.cishu; k ++ ){
              let i; //获取对应npc列表的位置
              for (i = 0; i < data.npc_list.length; i++) {
                if (data.npc_list[i].name == '万仙盟') {
                  break;
                }
              }
              let monster_length;
              let monster_index;
              let monster;
              if (weizhi.Grade == 1) {
                monster_length = data.npc_list[i].one.length;
                monster_index = Math.trunc(Math.random() * monster_length);
                monster = data.npc_list[i].one[monster_index];
              } else if (weizhi.Grade == 2) {
                monster_length = data.npc_list[i].two.length;
                monster_index = Math.trunc(Math.random() * monster_length);
                monster = data.npc_list[i].two[monster_index];
              } else {
                monster_length = data.npc_list[i].three.length;
                monster_index = Math.trunc(Math.random() * monster_length);
                monster = data.npc_list[i].three[monster_index];
              }
              //设定npc数值
              let B_player = {
                名号: monster.name,
                攻击: parseInt(monster.atk * A_player.攻击),
                防御: parseInt(
                    (monster.def * A_player.防御) / (1 + weizhi.Grade * 0.05)
                ),
                当前血量: parseInt(
                    (monster.blood * A_player.当前血量) / (1 + weizhi.Grade * 0.05)
                ),
                暴击率: monster.baoji,
                灵根: monster.灵根,
                法球倍率: monster.灵根.法球倍率,
              };
              let Random = Math.random();
              let npc_damage = Math.trunc(
                  Harm(B_player.攻击 * 0.85, A_player.防御) +
                  Math.trunc(B_player.攻击 * B_player.法球倍率) +
                  B_player.防御 * 0.1
              );
              if (Random < 0.1) {
                A_player.当前血量 -= npc_damage;
                last_msg +=
                    B_player.名号 +
                    '向镖车试探，丢出神通,剩余血量' +
                    A_player.当前血量;
              } else if (Random < 0.25) {
                A_player.当前血量 -= Math.trunc(npc_damage * 0.3);
                last_msg +=
                    '你散发剑气护住了' +
                    B_player.名号 +
                    '的镖车，伪装成【,' +
                    B_player.名号 +
                    '】的深渊刺客决定试探你,只用了三分力,剩余血量' +
                    A_player.当前血量;
              } else if (Random < 0.5) {
                A_player.当前血量 -= Math.trunc(npc_damage * 1.5);
                last_msg +=
                    '你的剑气引起了伪装成【' +
                    B_player.名号 +
                    '】的深渊刺客愤怒,【' +
                    B_player.名号 +
                    '】派出了更多的人,剩余血量' +
                    A_player.当前血量;
              } else if (Random < 0.7) {
                A_player.当前血量 -= Math.trunc(npc_damage * 1.3);
                last_msg +=
                    '你成功的吸引了所有的仇恨,伪装成【' +
                    B_player.名号 +
                    '】的刺客动用了深渊的力量,剩余血量' +
                    A_player.当前血量;
              } else if (Random < 0.9) {
                A_player.当前血量 -= Math.trunc(npc_damage * 1.8);
                last_msg +=
                    '离' +
                    B_player.名号 +
                    '越来越近，劫镖者越发着急了,剩余血量' +
                    A_player.当前血量;
              } else {
                A_player.当前血量 -= Math.trunc(npc_damage * 0.5);
                last_msg +=
                    '身体快到极限了嘛,你暗暗问道,这趟镖有难度啊,剩余血量' +
                    A_player.当前血量;
              }
              if (A_player.当前血量 < 0) {
                A_player.当前血量 = 0;
              }
              let arr = action;
              let shop = await Read_shop();
              for (i = 0; i < shop.length; i++) {
                if (shop[i].name == weizhi.name) {
                  shop[i].state = 0;
                  break;
                }
              }
              if (A_player.当前血量 <= 0) {
                var num = weizhi.Grade + 1;
                last_msg +=
                    '\n未能守护住镖车,被劫了扔在天牢\n在天牢中你找到了秘境之匙x' +
                    num;
                await Add_najie_thing(usr_qq, '秘境之匙', '道具', num);
                shop[i].state = 0;
                await Write_shop(shop);
                //写入redis
                await redis.del(
                    'xiuxian:player:' + usr_qq + ':action');
                await redis.del('xiuxian:player:' + weizhi.id + ':xijieend');
                arr.action = '禁闭';
                arr.start_time = new Date().getTime() ;
                //写入redis
                await redis.set(
                    'xiuxian:player:' + usr_qq + ':action',
                    JSON.stringify(arr)
                );
                msg.push('\n' + last_msg);
                e.reply(msg);
                return;
              }
            }
            last_msg += '\n你成功躲过了深渊的追杀,完美的护送了镖车';
            for (var j = 0; j < arr.thing.length; j++) {
              await Add_najie_thing(
                  usr_qq,
                  arr.thing[j].name,
                  arr.thing[j].class,
                  arr.thing[j].数量
              );
              last_msg += '';
            }
            shop[i].Grade++;
            if (shop[i].Grade > 3) {
              shop[i].Grade = 3;
            }
            shop[i].state = 0;
            await Write_shop(shop);
            await redis.del('xiuxian:player:' + usr_qq + ':action');
            await redis.del('xiuxian:player:' + weizhi.id + ':xijieend');
            msg.push('\n' + last_msg);
            e.reply(msg);
            return;
          } else if (msgg.find(item => item == B_win)) {
            var num = weizhi.Grade;
            last_msg +=
                ',\n未能守护住镖车,被劫了扔在天牢\n在天牢中你找到了秘境之匙x' +
                num;
            await Add_najie_thing(usr_qq, '秘境之匙', '道具', num);
            let shop = await Read_shop();
            for (i = 0; i < shop.length; i++) {
              if (shop[i].name == weizhi.name) {
                shop[i].state = 0;
                break;
              }
            }
            await Write_shop(shop);
            arr.action = '禁闭';
            arr.start_time = new Date().getTime();
            //写入redis
            await redis.set(
                'xiuxian:player:' + usr_qq + ':action',
                JSON.stringify(arr)
            );
            await redis.del('xiuxian:player:' + weizhi.id + ':xijieend');
            msg.push('\n' + last_msg);
            e.reply(msg);
            return;
          }
        }else{
          let time = parseInt((need_time-get_time)/60000);
          e.reply(`急什么急，这趟护送路途遥远，等${time}分钟`)
          return;
        }
      }else if('禁闭' == action.action){
        let need_time = 60*1000*60;
        if(need_time <= get_time){
          await redis.del('xiuxian:player:' + usr_qq + ':action');
          e.reply("碎了一觉，跑出来了");
          return;
        }else{
          let time = parseInt((need_time-get_time)/60000);
          e.reply(`继续禁闭${time}分钟`);
          return;
        }
      }
    }
    e.reply("搁这愁啥呢");
  }


